import 'package:crclib/crclib.dart';

const int AUTH_BASE = 0x5555AAAA;
const int AUTH_KEY = 123456;

enum DevAuthState {
  unknow,
  randomRequest,
  random,
  pubkeySend,
  pubkey,
  error,
  success,
}

class DevAuthResult {
  DevAuthResult({this.state = DevAuthState.unknow, this.value});

  DevAuthState state;
  String value;
}

/*
* c语言实现
*
uint32_t auth_pubkey(uint32_t rand, uint32_t key)
{
	uint32_t data[4];

	data[0] = AUTH_BASE;
	data[1] = key;
	data[2] = rand;
	data[3] = key + rand;
	key = (data[0] + data[1] + data[2]) * data[3];
	SYS_AUTH_DBG("key: 0x%lx\r\n", key);
	return key;
//	return crc32_compute((uint8_t*) data, sizeof(data), NULL);
}
*
* */
class Auth {
  static int pubkey(int rand, int key) {
    int data0 = AUTH_BASE;
    int data1 =  key;
    int data2 = rand;
    int data3 = key + rand;
    key = ((data0 + data1 + data2) * data3) & 0xFFFFFFFF;
    return key;
  }
}
